using System;
using System.Globalization;

using System.Data;
using System.Text ;
using System.Collections ;
using System.Windows.Forms;

using WP.MF.DataAccess;
using WP.DB;
using WP.MF.DataEntity;
using WP.Commons;

namespace WP.MF.BusinessRule
{
	/// <summary>
	/// Summary description for MMenuBRL.
	/// </summary>
	public class MMenuBRL : RuleBase
	{
		public MMenuBRL()
		{
			//
			// TODO: Add constructor logic here
			//
		}
		#region Insert into MMenu Valid
		public bool InsertMenuValid(DBAccess dBAccess, MMenuDEL mMenuDEL)
		{
			bool bReturn=true;
            string strErrPrompt = string.Empty;
            SysVar.ErrorMessage.Clear();

			MMenuDAL mMenuDAL=new MMenuDAL();
			DataRow DR=mMenuDEL.Tables[MMenuDEL.TABLENAME_MMENU].Rows[0];

            DataView dvwCheck = dBAccess.GetDataTable("select * from mnamedict where NeedCheck=1 and tablename='" + MMenuDEL.TABLENAME_MMENU + "'").DefaultView;
            foreach (DataRowView drw in dvwCheck)
            {
                if (!FieldCheck(dBAccess, mMenuDEL.Tables[MMenuDEL.TABLENAME_MMENU].Rows[0], drw["FieldName"].ToString(), ref strErrPrompt, FormStatus.AddNew))
                {
                    bReturn = false;
                    if (SysVar.ErrorMessage.Count == 0)
                    {
                        SysVar.ErrorMessage.Add(new ErrInfo(strErrPrompt, drw["FieldName"].ToString()));
                    }
                    break;
                }
            }
			if(!CommonValid(DR))
			{
				bReturn=false;
			}

			return bReturn;
		}
		#endregion

		#region Update MMenu Valid
		public bool UpdateMenuValid(DBAccess dBAccess,MMenuDEL mMenuDEL)
		{
			bool bReturn=true;
            string strErrPrompt = string.Empty;
			string strRecId = string.Empty;
			SysVar.ErrorMessage.Clear();

            if (IsExistedAndNotChanged(dBAccess,mMenuDEL.Tables[MMenuDEL.TABLENAME_MMENU]))
			{
				DataRow DR=mMenuDEL.Tables[MMenuDEL.TABLENAME_MMENU].Rows[0];
				strRecId=DR[MMenuDEL.RECID].ToString();

                DataView dvwCheck = dBAccess.GetDataTable("select * from mnamedict where NeedCheck=1 and tablename='" + MMenuDEL.TABLENAME_MMENU + "'").DefaultView;
                foreach (DataRowView drw in dvwCheck)
                {
                    if (!FieldCheck(dBAccess, mMenuDEL.Tables[MMenuDEL.TABLENAME_MMENU].Rows[0], drw["FieldName"].ToString(), ref strErrPrompt, FormStatus.Update))
                    {
                        bReturn = false;
                        if (SysVar.ErrorMessage.Count == 0)
                        {
                            SysVar.ErrorMessage.Add(new ErrInfo(strErrPrompt, drw["FieldName"].ToString()));
                        }
                        break;
                    }
                }
				if(IsRepeatUpdate(dBAccess,MMenuDEL.TABLENAME_MMENU,strRecId,MMenuDEL.MENUID,DR[MMenuDEL.MENUID].ToString()))
				{
					bReturn=false;
				}
				if(!CommonValid(DR))
				{
					bReturn=false;

				}
			}
            else
            {
                bReturn = false;
            }
			
			return bReturn;
		}

		#endregion

		#region Delete MMenu Valid
        public bool DeleteMMenuValid(DBAccess dBAccess, string strRecId, byte[] btOldRecStamp)
		{
			SysVar.ErrorMessage.Clear();
            if (!base.IsExistedAndNotChanged(dBAccess, MMenuDEL.TABLENAME_MMENU, strRecId, btOldRecStamp))
			{
				return false;
			}

            if (MsgBoxEx.Show(Msg.Select("Are you sure to delete this Menu and its SubMenu?", DataFunctions.GetMsg(this.GetType().Name, "MSG_DeleteMenuAndItsSubMenu")), SysVar.ConfirmTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No)
            {
                return false;
            }
			return true;
		}

		#endregion

        #region Common valid
        private bool CommonValid(DataRow drMMenuDEL)
		{
			if(!BaseFunctions.IsNumeric(drMMenuDEL[MMenuDEL.MENUID].ToString()) || drMMenuDEL[MMenuDEL.MENUID].ToString().Length!=12)
			{
                SysVar.ErrorMessage.Add(Msg.Select("the format of MenuId must be xxx-xxx-xxx-xxx. (x represents a digit,length is 12)", DataFunctions.GetMsg(this.GetType().Name, "MSG_FormatOfMenuID")));
				return false;
			}
			else
			{
				return true;
			}

		}
		#endregion

        #region Field Check
        public bool FieldCheck(DBAccess dbcon, DataRow dr, string strFieldName, ref string strErrPrompt, FormStatus formStatus)
        {
            strErrPrompt = string.Empty;
            if (strFieldName == null) return true;

            bool blnExistsField = false;
            for (int i = 0; i < dr.Table.Columns.Count; i++)
            {
                if (dr.Table.Columns[i].Caption.ToUpper().Trim() == strFieldName.Trim().ToUpper())
                {
                    blnExistsField = true;
                    break;
                }
            }
            if (!blnExistsField) return true;
            string strFieldValue = CvtFunc.ToString(dr[strFieldName.Trim()]);

            switch (strFieldName.ToUpper().Trim())
            {
                #region MenuID
                case "MENUID":
                    if (formStatus == FormStatus.AddNew)
                        return RuleBase.FieldValueCheckOne(dbcon, strFieldValue, MMenuDEL.TABLENAME_MMENU, strFieldName, ref  strErrPrompt);
                    else
                        return true;
                #endregion

                default:
                    return RuleBase.FieldValueCheckOne(dbcon, strFieldValue, MMenuDEL.TABLENAME_MMENU, strFieldName, ref strErrPrompt);
            }
        }
        #endregion
	}
}
